﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using System.IO;
using System.Xml;
using System.Windows.Shell;
using Microsoft.WindowsAPICodePack.Shell;

namespace KazhOo_Lil_Launcher
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            GenerateJumpList2();

            InitializeComponent();
        }

        private void GenerateJumpList()
        {
            JumpList jumpList = new JumpList();
            jumpList.JumpItemsRejected += new EventHandler<JumpItemsRejectedEventArgs>(jumpList_JumpItemsRejected);

            string[] Files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.System), "*.msc");
            foreach (string File in Files)
            {
                XmlDocument XmlDocument = new XmlDocument();

                XmlDocument.Load(File);

                JumpTask task = new JumpTask();

                task.Title = System.IO.Path.GetFileNameWithoutExtension(File);
                task.Description = task.Title;

                XmlNode XmlNodeApplicationTitle = XmlDocument.SelectSingleNode("//VisualAttributes/String[@Name='ApplicationTitle']");
                if (XmlNodeApplicationTitle != null)
                {
                    int ApplicationTitle = -1;
                    int.TryParse(XmlNodeApplicationTitle.Attributes["ID"].Value, out ApplicationTitle);

                    XmlNode XmlNodeApplicationID = XmlDocument.SelectSingleNode("//Strings/String[@ID='" + ApplicationTitle.ToString() + "']");
                    if (XmlNodeApplicationID != null)
                    {
                        task.Title = XmlNodeApplicationID.InnerText;
                        task.Description = task.Title;
                    }
                }

                XmlNode XmlNode = XmlDocument.SelectSingleNode("//VisualAttributes/Icon");
                if (XmlNode != null)
                {
                    int IconResourceIndex;
                    int.TryParse(XmlNode.Attributes["Index"].Value, out IconResourceIndex);
                    task.IconResourceIndex = IconResourceIndex;

                    string IconFile = XmlNode.Attributes["File"].Value;
                    int pos = IconFile.ToUpper().IndexOf("\\SYSTEM32");
                    task.IconResourcePath = "%systemroot%" + IconFile.Substring(pos);
                }
                else
                {
                    task.IconResourceIndex = 0;
                    task.IconResourcePath = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\mmc.exe";
                }

                task.ApplicationPath = File;

                jumpList.JumpItems.Add(task);
            }

            jumpList.ShowFrequentCategory = false;
            jumpList.ShowRecentCategory = false;
            JumpList.SetJumpList(Application.Current, jumpList);

            this.DataContext = jumpList;
        }

        private void GenerateJumpList2()
        {
            JumpList jumpList = new JumpList();
            jumpList.JumpItemsRejected += new EventHandler<JumpItemsRejectedEventArgs>(jumpList_JumpItemsRejected);

            ShellContainer System32Folder = (ShellContainer)KnownFolders.System;
            
            List<ShellFile> MSCFiles = new List<ShellFile>();

            foreach (ShellFile ShellFile in System32Folder.OfType<ShellFile>())
            {
                if (System.IO.Path.GetExtension(ShellFile.Path).ToLower() == ".msc")
                {
                    XmlDocument XmlDocument = new XmlDocument();

                    XmlDocument.Load(ShellFile.Path);

                    JumpTask task = new JumpTask();

                    task.Title = System.IO.Path.GetFileNameWithoutExtension(ShellFile.Path);
                    task.Description = task.Title;

                    XmlNode XmlNodeApplicationTitle = XmlDocument.SelectSingleNode("//VisualAttributes/String[@Name='ApplicationTitle']");
                    if (XmlNodeApplicationTitle != null)
                    {
                        int ApplicationTitle = -1;
                        int.TryParse(XmlNodeApplicationTitle.Attributes["ID"].Value, out ApplicationTitle);

                        XmlNode XmlNodeApplicationID = XmlDocument.SelectSingleNode("//Strings/String[@ID='" + ApplicationTitle.ToString() + "']");
                        if (XmlNodeApplicationID != null)
                        {
                            task.Title = XmlNodeApplicationID.InnerText;
                            task.Description = task.Title;
                        }
                    }

                    XmlNode XmlNode = XmlDocument.SelectSingleNode("//VisualAttributes/Icon");
                    if (XmlNode != null)
                    {
                        int IconResourceIndex;
                        int.TryParse(XmlNode.Attributes["Index"].Value, out IconResourceIndex);
                        task.IconResourceIndex = IconResourceIndex;

                        string IconFile = XmlNode.Attributes["File"].Value;
                        int pos = IconFile.ToUpper().IndexOf("\\SYSTEM32");
                        task.IconResourcePath = "%systemroot%" + IconFile.Substring(pos);
                    }
                    else
                    {
                        task.IconResourceIndex = 0;
                        task.IconResourcePath = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\mmc.exe";
                    }

                    task.ApplicationPath = ShellFile.Path;

                    jumpList.JumpItems.Add(task);
                }
            }

            jumpList.ShowFrequentCategory = false;
            jumpList.ShowRecentCategory = false;
            JumpList.SetJumpList(Application.Current, jumpList);

            this.DataContext = jumpList;
        }


        void jumpList_JumpItemsRejected(object sender, JumpItemsRejectedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0} Jump Items Rejected:\n", e.RejectionReasons.Count);
            for (int i = 0; i < e.RejectionReasons.Count; ++i)
            {
                if (e.RejectedItems[i].GetType() == typeof(JumpPath))
                    sb.AppendFormat("Reason: {0}\tItem: {1}\n", e.RejectionReasons[i], ((JumpPath)e.RejectedItems[i]).Path);
                else
                    sb.AppendFormat("Reason: {0}\tItem: {1}\n", e.RejectionReasons[i], ((JumpTask)e.RejectedItems[i]).ApplicationPath);
            }

            MessageBox.Show(sb.ToString());
        }

        private void ThumbButtonInfo_Click(object sender, EventArgs e)
        {

        }

        private void ThumbButtonInfo_Click_1(object sender, EventArgs e)
        {

        }
    }
}
